package com.zelma.mywebsocket.ws;

import com.alibaba.fastjson.JSON;
import com.zelma.mywebsocket.config.Rediss;
import com.zelma.mywebsocket.dao.AgentDao;
import com.zelma.mywebsocket.pojo.Message;
import com.zelma.mywebsocket.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ServerEndpoint(value = "/char",configurator = GetHttpSession.class)
@Component
public class CharEndPoint {
    @SuppressWarnings("all")
    @Autowired
    AgentDao agentDao;
    @Autowired
    StringRedisTemplate redisTemplate;
    //所有EndPoint对象
    private static Map<String, CharEndPoint> endPointMap=new HashMap<>();
    //每个session
    private Session session;
    //每个httpSession
    private HttpSession httpSession;

    @OnOpen
    public void onopen(Session session, EndpointConfig endpointConfig){
        this.session=session;
        //获取当前httpsession
        HttpSession httpSessions= (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());
        this.httpSession=httpSessions;
        //获取当前对象名字
        String user = (String) httpSessions.getAttribute("user");
        endPointMap.put(user,this);
        //获取所有的名称
        Set<String> strings = endPointMap.keySet();



        String message="";
        String messages="";
        //查询所有登录的经纪人
        List<String> agents = (List<String>) httpSessions.getAttribute("agents");
        message = MessageUtils.getMessage(true, null, agents,"agent");
        //查询所有登录用户
        List<String> users = (List<String>) httpSessions.getAttribute("users");
        messages = MessageUtils.getMessages(true, null, users,"user");
        //名称给封装类
        //要给所有enpoint发送信息
        if (!StringUtils.isEmpty(message)){
            try {
                for (String string : strings) {
                    CharEndPoint charEndPoint = endPointMap.get(string);
                    charEndPoint.session.getBasicRemote().sendText(message);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(messages)){
            try {
                for (String string : strings){
                    CharEndPoint charEndPoint = endPointMap.get(string);
                    charEndPoint.session.getBasicRemote().sendText(messages);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("用户"+user+"连接成功");
    }
    @OnMessage
    public void onMessage(String message,Session session){
        Message message1 = JSON.parseObject(message, Message.class);
        String message2 = message1.getMessage();
        String toName = message1.getToName();
        CharEndPoint charEndPoint = endPointMap.get(toName);
        String user = (String) httpSession.getAttribute("user");
        String status = (String) httpSession.getAttribute("status");
        String message3="";
        message3 = MessageUtils.getMessage(false, user, message2,"user");
        try {
            charEndPoint.session.getBasicRemote().sendText(message3);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @OnClose
    public void onclone(Session session){

    }





}
